package com.augurit.agmobile.agwater5.drainage.supervise.adapter

import android.graphics.Rect
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.RecyclerView
import com.augurit.agmobile.agwater5.drainage.common.ex.toColorSpan
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseVariousItemBean
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseVariousSubModuleBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.ext.dpInt
import com.example.drainage.R
import com.example.drainage.base.ktexpand.visible

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.supervise.adapter
 * @createTime 创建时间 ：2022/1/5
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class SuperviseVariousSubModuleAdapter :
    BaseQuickAdapter<SuperviseVariousSubModuleBean, BaseViewHolder>(R.layout.item_supervise_various_sub_module) {

    private val countColor = "#FF2B2F36".toColorInt()

    override fun convert(holder: BaseViewHolder, item: SuperviseVariousSubModuleBean) {
        holder.setText(R.id.tvSubModuleName, item.subModuleName)
        val countTextView = holder.getView<TextView>(R.id.tvSubModuleCount)
        countTextView.text = getModuleCountChar(item.subModuleTotalCount, item.subModuleSuperviseCount)
        val ivCollapsed = holder.getView<ImageView>(R.id.ivCollapsed).apply {
            setImageResource(if (item.isExpend) R.mipmap.icon_supervise_various_collapsed else R.mipmap.icon_supervise_various_expanded)
            setOnClickListener {
                val clickItem = getItem(holder.absoluteAdapterPosition)
                clickItem.isExpend = !clickItem.isExpend
                if (clickItem.isExpend) {
                    fixHideForRecyclerView(recyclerView, holder.itemView, clickItem.itemList?.size ?: 4)
                }
                notifyItemChanged(holder.absoluteAdapterPosition)
            }
        }
        countTextView.setOnClickListener { ivCollapsed.performClick() }

        val recyclerView = holder.getView<RecyclerView>(R.id.subModuleRecyclerView)
        val isShowRecyclerView = item.subModuleObj != null && item.isExpend
        recyclerView.visible = isShowRecyclerView
        if (isShowRecyclerView) {
            val adapter = if (recyclerView.adapter == null) {
                SuperviseVariousItemAdapter().also {
                    recyclerView.adapter = it
                }
            } else {
                recyclerView.adapter as SuperviseVariousItemAdapter
            }
            if (item.itemList == null) {
                // 数据转换
                item.itemList = mutableListOf(
                    SuperviseVariousItemBean("重要小型水库未明确水库管理机构重要小型水库未明确水库管理机构", "未检查", 1),
                    SuperviseVariousItemBean("一般小型水库无专人管理", "正常", 2),
                    SuperviseVariousItemBean("信息档案管理", "一般", 3),
                    SuperviseVariousItemBean("信息档案管理", "较重", 4),
                    SuperviseVariousItemBean("信息档案管理", "严重", 5),
                    SuperviseVariousItemBean("信息档案管理", "特别严重", 6),
                    SuperviseVariousItemBean("水库管理制度不满足运行管理工作", "合理缺项", 7),
                )
            }
            adapter.setNewInstance(item.itemList)
        }
    }

    private fun fixHideForRecyclerView(recyclerView: RecyclerView?, itemView: View, size: Int) {
        recyclerView ?: return
        recyclerView.parent.parent.takeIf {
            it is RecyclerView
        }?.let {
            it as RecyclerView
        }?.apply {
            val rectParent = Rect()
            val rect = Rect()
            getGlobalVisibleRect(rectParent)
            itemView.getGlobalVisibleRect(rect)

            val offset = rectParent.bottom - rect.bottom
            if (offset < height / 5) {
                postDelayed({
                    smoothScrollBy(0, size * 40.dpInt)
                }, 250)
            }
        }
    }

    private fun getModuleCountChar(totalCount: Int, superviseCount: Int): CharSequence {
        val str = "${superviseCount}/${totalCount}"
        return if (superviseCount == 0) {
            str
        } else {
            val superviseLength = superviseCount.toString().length
            str.toColorSpan(0..superviseLength, countColor)
        }
    }
}